﻿using Admin2024.Domain.Entity;
using Admin2024.Domain.Interface;
using Admin2024.Domain.ObjValue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Domain.DomainService.System
{
    public class BaseDomainService<T> : IBaseDomainService<T> where T : BaseEntity
    {
        private readonly IRepository<T> _entityRepository;


        public BaseDomainService(IRepository<T> entityRepository)
        {
            _entityRepository = entityRepository;
        }

        //删除(真)
        public async Task<DomainResult<T>> DeleteAsync(Guid id)
        {
            var entity = await _entityRepository.GetByIdAsync(id);
            if (entity == null)
            {
                return DomainResult<T>.Error("查无此人");
            }
            if (entity.IsDeleted == true)
            {
                return DomainResult<T>.Error("其实已经删除了这个人了,你还删干嘛");
            }
            await _entityRepository.DeleteAsync(id);

            return DomainResult<T>.Success(entity);
        }
        //批量删除
        public Task<DomainResult<IEnumerable<T>>> DeleteRangeAsync(IEnumerable<Guid> ids)
        {
            throw new NotImplementedException();
        }

        //删除(软)
        public async Task<DomainResult<T>> DisableAsync(Guid id)
        {
            var entity = await _entityRepository.GetByIdAsync(id);
            if (entity == null)
            {
                return DomainResult<T>.Error("查无此人");
            }

            entity.IsDeleted = !entity.IsDeleted;
            var entit = await _entityRepository.UpdateAsync(entity);

            return DomainResult<T>.Success(entit);

        }
        //是否启用  (启用就变不启用）,（不启用就变启用）
        public async Task<DomainResult<T>> EnableAsync(Guid id)
        {
            var entity = await _entityRepository.GetByIdAsync(id);
            if (entity == null)
            {
                return DomainResult<T>.Error("查无此人");
            }

            if (entity != null)
            {
                entity.IsActived = !entity.IsActived;
                entity = await _entityRepository.UpdateAsync(entity);
                return DomainResult<T>.Success(entity);
            }

            return DomainResult<T>.Error("修改失败了");
        }

        // 设置显示顺序
        public async Task<DomainResult<T>> SetDisplayOrderAsync(Guid id, int orderNumber)
        {
            var entity = await _entityRepository.GetByIdAsync(id);
            if (entity == null)
            {
                return DomainResult<T>.Error("查无此人");
            }

            if (entity.IsDeleted == true)
            {
                return DomainResult<T>.Error("角色已被禁用");
            }
            entity.DisplayOrder = orderNumber;
            entity = await _entityRepository.UpdateAsync(entity);
            return DomainResult<T>.Success(entity);

        }

        // 设置备注
        public async Task<DomainResult<T>> SetRemarksAsync(Guid id, string remarks)
        {
            var entity = await _entityRepository.GetByIdAsync(id);
            if (entity == null)
            {
                return DomainResult<T>.Error("查无此人");
            }

            if (entity.IsDeleted == true)
            {
                return DomainResult<T>.Error("角色已被禁用");
            }
            entity.Remarks = remarks;
            entity = await _entityRepository.UpdateAsync(entity);
            return DomainResult<T>.Success(entity);

        }
    }
}
